﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Wise.Util.Queryable
{
    /// <summary>
    /// 基础实体信息
    /// 如非特殊需要，请继承BaseEntityInfo
    /// 用途：
    /// 1.集成属性变动事件
    /// 2.便于后期无代码维护(事件驱动)
    /// </summary>
    [Serializable, EditorBrowsable(EditorBrowsableState.Never)]
    public abstract partial class _Entity : INotifyPropertyChanged, INotifyPropertyChanging
    {
        public const string _无法识别 = "无法识别";

        /// <summary>
        /// 基础实体信息
        /// </summary>
        public _Entity()
        {
            OnCreated();
        }

        /// <summary>
        /// 当修改后,自动修改 更新日期
        /// </summary>
        [EditorBrowsable(EditorBrowsableState.Never), Browsable(false)]
        protected virtual void OnModified() { }


        #region INotifyPropertyChanged,INotifyPropertyChanging

        /// <summary>
        /// 属性变动后的事件
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        /// <summary>
        /// 属性变动前
        /// </summary>
        public event PropertyChangingEventHandler PropertyChanging;

        /// <summary>
        /// 触发属性变动后的事件
        /// </summary>
        /// <param name="propertyName"></param>
        protected internal virtual void SendPropertyChanged(String propertyName)
        {
            OnModified();
            if (OnPropertyChanged(propertyName) && (this.PropertyChanged != null))
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        /// <summary>
        /// 在属性变化时
        /// 在PropertyChanged事件发生之前触发，返回false将阻止PropertyChanged事件
        /// </summary>
        /// <param name="propertyName"></param>
        protected virtual bool OnPropertyChanged(string propertyName)
        {
            return true;
        }

        /// <summary>
        /// 在属性变化时
        /// 在PropertyChanged事件发生之前触发，返回false将阻止PropertyChanged事件
        /// </summary>
        protected virtual bool OnBeforePropertyChanged(string propertyName, object newValue)
        {
            return true;
        }

        private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty);
        #endregion

        #region 可扩展性方法定义
        partial void OnLoaded();
        partial void OnValidate(Action<string, object> action);
        partial void OnCreated();

        #endregion

    }
}
